package com.a16.yuejian.yuejian.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.PostModel;

public class PostReleaseActivity extends AppCompatActivity implements View.OnClickListener {
     private ImageView post_release_back,post_release_publish;
     private TextView post_release;
     private EditText post_release_content;
    private String picPath=null;
    private SharedPreferences sp;

    SuccessListener successListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(PostReleaseActivity.this,"发布成功", Toast.LENGTH_SHORT).show();
                finish();
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(PostReleaseActivity.this,"发布失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_release);
        initView();
        initEvent();
    }

    private void initEvent() {
        post_release_back.setOnClickListener(this);
        post_release_publish.setOnClickListener(this);
        post_release.setOnClickListener(this);
    }

    private void initView() {
        post_release_back = (ImageView)findViewById(R.id.post_release_back);
        post_release_publish = (ImageView)findViewById(R.id.post_release_publish);
        post_release_content = (EditText)findViewById(R.id.post_release_content);
        post_release = (TextView)findViewById(R.id.post_release);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.post_release_back:
                finish();
                break;
            case R.id.post_release_publish:
                final CharSequence[] items = {"相册"};
                new AlertDialog.Builder(PostReleaseActivity.this).setTitle("选择获取图片方式").setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which==0){
                            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                            intent.setType("image/*");
                            startActivityForResult(intent,1);
                        }
                    }
                }).create().show();
                break;
            case R.id.post_release:
                PostModel postModel = new PostModel();
                postModel.send(sp.getString("uid",""),post_release_content.getText().toString(),picPath,successListener);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Activity.RESULT_OK){
            switch (requestCode){
                case 1:
                    if (data != null) {
                        Uri uri = data.getData();
                        post_release_publish.setImageURI(uri);
                        ContentResolver cr = this.getContentResolver();
                        Cursor c = cr.query(uri, null, null, null, null);
                        c.moveToFirst();
                        //这是获取的图片保存在sdcard中的位置
                        picPath = c.getString(c.getColumnIndex("_data"));
                    } else {
                        return;
                    }
                    break;
            }
        }
    }
}
